package com.seanLab.tool.TagSuggestModel.ExpandRank;

import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * 根据idf调整TextRank最终返回分数（adjustRank），idf大于50000时分数将为原来的1/3
 */
public class IDFTextRankKE extends FakeExpandRankKE{
    private static Logger LOG = Logger.getLogger(IDFTextRankKE.class.getName());
    private static int docNum = 0;
    private static int max = 0;
    private static ConcurrentHashMap<String, Integer> idf = null;

    private void sortList(ConcurrentHashMap<String, Integer> map) {
        List<Map.Entry<String, Integer>> list = new ArrayList<>();
        list.addAll(map.entrySet());
        Collections.sort(list , new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        try {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("workingDir/idf_20180321_sort.dat")));
            writer.write(docNum + "\n");
            for (Map.Entry<String, Integer> entry : list) {
                writer.write(entry.getKey() + "\t" + entry.getValue() + "\n");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void loadModel(String modelPath) throws IOException {
        if (idf == null) {
            LOG.info("idf text rank loading!");
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(modelPath)));
            docNum = Integer.parseInt(reader.readLine());
            idf = new ConcurrentHashMap<>(docNum + 1);
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] item = line.split("\t");
                int value = Integer.parseInt(item[1]);
                if (value > max) {
                    max = value;
                }
//                if (value > 50000) {
//                    System.out.println(item[0]);
//                }
                idf.put(item[0], value);
            }
            LOG.info("idf text rank loaded! Max = " + max);
        }
    }

    public double adjustRank(String word, double score) {
        if (idf.containsKey(word)) {
            int count = idf.get(word);

            if (count > 50000) {
                return score * 0.3;
            }

//            return score * Math.log10(docNum / count);
        }
        return score;
    }
}
